package viewdraw.zjitc.net.androidhomeworkviewdraw;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author 朱志文
 * @data 2019/10/15 0015
 */
public class MyDraw extends View {
    public MyDraw(Context context) {
        super(context);
    }

    public MyDraw(Context context,  AttributeSet attrs) {
        super(context, attrs);
    }

    public MyDraw(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.dm1);
        float bitMapHeight=bitmap.getHeight();
        float bitMapWidth=bitmap.getWidth();
        float width=getWidth();
        float height=(width/bitMapWidth)*bitMapHeight;

        Bitmap createBitmap=Bitmap.createBitmap((int)width,(int)height,Bitmap.Config.ARGB_8888);

        Paint paint = new Paint();
        Canvas canvas1 = new Canvas(createBitmap);
        paint.setAntiAlias(true);
        //在画布上绘制圆形
        canvas1.drawCircle(300,300,100,paint);
        //PorterDuffXfermode(PorterDuff.Mode.SRC_IN) 保持目标像素的源像素，丢弃剩余的源像素和目标像素
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas1.drawBitmap(bitmap, null, new RectF(0, 0, width, height), paint);
        canvas.drawBitmap(createBitmap, 0, 0, new Paint());


    }

}
